package com.yhd.tomcat.handler.connection;

import com.yhd.tomcat.Bootstrap;
import com.yhd.tomcat.handler.Handler;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;

/**
 * @author yhd
 * @email yinhuidong1@xiaomi.com
 * @description TODO
 * @since 2021/6/10 下午3:48
 */
public class ConnectionHandler extends Handler {

    private static volatile ConnectionHandler connectionHandler;

    private ServerSocket serverSocket;



    private ConnectionHandler() {
        try {
            System.out.println("创建服务器连接");
            this.serverSocket = new ServerSocket();
            serverSocket.bind(new InetSocketAddress("localhost",PORT));
            System.out.println("Tomcat is starting  Listening port is "+PORT+"。");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static ConnectionHandler getSingleton(){
        if (connectionHandler==null){
            synchronized (Bootstrap.class){
                if (connectionHandler==null){
                    connectionHandler=new ConnectionHandler();
                }
            }
        }
        return connectionHandler;
    }

    public ServerSocket getServerSocket() {
        return serverSocket;
    }
}
